<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class Product extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'passport/index' );
			exit ();
		}
	}
	/*
	 * 返回商品列表
	 */
	public function index() {
		$category = $this->uri->segment(4);
    	$shelve = $this->uri->segment(6);
		$name = $this->uri->segment(8);
		$name=urldecode($name);//转化为汉字
		if(empty($category)){
    		$category=0;
    	}
		if(empty($shelve)){
    		$shelve=0;
    	}
		if(empty($name)){
    		$name="empty";
    	}
		$data ['category']=$category;
		$data ['shelve']=$shelve;
    	$this->load->library ( 'pagination' );
    	// 每页显示数据条数
    	$per_page=10;//每页显示10项

    	$config['base_url']=site_url('product/index/category/'.$category.'/shelve/'.$shelve.'/name/'.$name);
    	$this->load->model ( 'product_model' );
		$this->load->model ( 'category_model' );
		
		if($name=="empty"){//这里是为了搜索
    		$name=null;
    	}
    	// 数据总数
    	$config ['total_rows'] = $this->product_model->count_products($category,$shelve,$name);
    	$config ['per_page'] = $per_page;
    	$config ['first_link'] = '第一页';
    	$config ['last_link'] = '最后一页';
    	$config ['next_link'] = '下一页';
    	$config ['prev_link'] = '上一页';
    	$config['uri_segment'] =9;

    	$this->pagination->initialize($config);
    	$data['links']=$this->pagination->create_links();
    	//偏移量
    	$offset=intval($this->uri->segment(9));
    	$data ['products'] = $this->product_model->get_products($category,$shelve,$name,$per_page,$offset);
		$data ['categories'] = $this->category_model->find_all_categories();
    	$data ['title'] = '商品列表 - '.SYSTEM_NAME;
    	$this->load->view ( 'header', $data );
    	$this->load->view ( 'product/index' );
    	$this->load->view ( 'footer' );
	}
	/*
	 * 跳转到添加商品页面
	 */
	public function add() {
		$this->load->model ( 'category_model' );
		$data ['categories'] = $this->category_model->find_all_categories();
		$data ['title'] = '添加商品 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'product/add' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 跳转到编辑商品页面
	 */
	public function edit() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'category_model' );
		$data ['categories'] = $this->category_model->find_all_categories ();
		$this->load->model ( 'product_model' );
		$data ['product'] = $this->product_model->load ( $id );
		$data ['title'] = '编辑商品 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'product/edit' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 保存商品信息
	 */
	public function save() {
		$id = $this->input->post('id');
		$newproduct = array (
			'image' => $this->input->post ( 'image' ),
			'name' => $this->input->post ( 'name' ),
			'categoryid' => $this->input->post ( 'categoryid' ),
			'price' => $this->input->post ( 'price' ),
			'unit' => $this->input->post ( 'unit' ) ,
			'description' => $this->input->post ( 'description' ),
			'week' => $this->input->post ( 'week' ),
			'select' => $this->input->post ( 'select' ),
			'daylim' => $this->input->post ( 'daylim' )
		);
		$this->load->model ( 'product_model' );
		if ($id) {
			if($this->product_model->update ( $id, $newproduct )){
				return_json(TRUE,'','修改成功！');
			}else{
				return_json(FALSE,'','信息未改动！');
			}
		} else {
			$productid=$this->product_model->create ( $newproduct );
			if(!empty($productid)){
				return_json(TRUE,'','添加成功！');
			}else{
				return_json(FALSE,'','添加失败！');
			}
		}
	}
	/*
	 * 删除商品信息
	 */
	public function delete() {
	    $id = $this->input->post('id');
		$this->load->model ( 'product_model' );
		if ($this->product_model->delete ( $id )) {//要不要考虑订单里有这个产品
			return_json(TRUE,'','删除成功！');
	    }else{
	    	return_json(FALSE,'','删除失败！');
	    }
    }
	/*
	 *上/下架商品
	 */
    public function setstate(){
    	$productid = $this->input->post ( 'productid' );
    	$state = $this->input->post ( 'state' );
    	$this->load->model ( 'product_model' );
    	$newproduct = array (
    			'shelve' => $state
    	);
    	if ($this->product_model->update ( $productid, $newproduct )) {
    		return_json ( TRUE, '', '操作成功！' );
    	}else{
    		return_json ( FALSE, '', '操作失败！' );
    	}
    }
	/*
	 * 跳转到添加配方页面
	 */
	public function materialindex() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'product_model' );
		$data ['product']=$this->product_model->load ( $id );
		$this->load->model ( 'compose_model' );
		$data ['materials']=$this->compose_model->find_by_product($id);
		$data ['title'] = '添加配方 - '.SYSTEM_NAME;
		$this->load->view ( 'header', $data );
		$this->load->view ( 'product/materialindex' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 添加配方信息
	 */
	public function addmaterial() {
		$product= $this->input->post ( 'product' );
		$material = $this->input->post ( 'material' );
		$amount = $this->input->post ( 'amount' );
		if(empty($material)){
			return_json ( FALSE, '', '请重新选择配方！' );
		}
		$this->load->model ( 'compose_model' );
		$compose = array (
			'productid' => $product,
			'materialid' => $material,
			'amount' => $amount,
		);
		$composeid=$this->compose_model->create ( $compose )	;
		if(!empty($composeid)){
			return_json ( TRUE, '', '添加成功！' );
		}else{
			return_json ( FALSE, '', '添加失败！' );
		}
	}
	/*
	 * 删除配方信息
	 */
	public function deletematerial() {
		$id = $this->input->post('id');
		$this->load->model ( 'compose_model' );
		if ($this->compose_model->delete( $id )) {
			return_json(TRUE,'','删除成功！');
	    }else{
	    	return_json(FALSE,'','删除失败！');
	    }
	}
	 /*
	 * 根据商品名称返回商品列表
	 */
	 public function get_by_name() {
    	$name = $this->input->post('name');
		$page = $this->input->post('page');
		$offset = $this->input->post('offset');
    	$this->load->model ( 'product_model' );
    	$products=$this->product_model->find_by_name ( $name,$page,$offset );
		$data['products']=$products;
		$data['num']=$this->product_model->count_by_name ( $name );
    	return_json ( TRUE,$data, '返回商品列表成功！' );
    }
}
